package top.i89ck.controller;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.ext.cors.EnableCORS;
import org.opensource.jfhelper.annocation.Get;
import org.opensource.jfhelper.annocation.Post;
import org.opensource.jfhelper.annocation.View;
import org.opensource.jfhelper.utils.Ret;
import top.i89ck.service.IndexService;

import javax.annotation.Resource;

@Path("/")
@EnableCORS
public class IndexController extends Controller {

    @Resource
    private IndexService indexService;

    /**
     * 返回json格式的数据
     *
     * @return
     */
    public Ret index() {
        return Ret.ok("hello jfinal!");
    }


    /**
     * 返回html页面渲染
     *
     * @return
     */
    @View
    public String hello() {
        return "hello.html";
    }

    /**
     * 测试请求方式
     *
     * @return
     */
    @Get
    public Ret get() {
        return Ret.ok("只能用Get请求");
    }

    /**
     * 测试请求方式
     *
     * @return
     */
    @Post
    public Ret post() {
        return Ret.ok("只能用Post请求");
    }

    /**
     * 测试请求方式  使用jfinal的@ActionKey注解
     *
     * @return
     */
    @ActionKey("/useAction")
    public Ret useAction() {
        return Ret.ok("只能用Get请求");
    }

    @Get
    @ActionKey("/testComponent")
    public Ret testComponent(){
        String message = indexService.sayHello();
        return Ret.ok().setData(message);
    }
}
